package com.cls.business.service.impl;

import com.cls.business.vo.EvaluationResultVO;
import com.cls.common.entity.QueryRequest;
import com.cls.business.entity.DeclarePlan;
import com.cls.business.mapper.DeclarePlanMapper;
import com.cls.business.service.IDeclarePlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;

/**
 *  Service实现
 *
 * @author slx
 * @date 2020-12-11 10:39:22
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DeclarePlanServiceImpl extends ServiceImpl<DeclarePlanMapper, DeclarePlan> implements IDeclarePlanService {

    @Override
    public PageInfo<DeclarePlan> findDeclarePlans(QueryRequest request, DeclarePlan declarePlan) {
        LambdaQueryWrapper<DeclarePlan> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        if(declarePlan.getDeclareId()!=null){
            queryWrapper.eq(DeclarePlan::getDeclareId,declarePlan.getDeclareId());
        }
        List<DeclarePlan> list= this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<DeclarePlan> findDeclarePlans(DeclarePlan declarePlan) {
	    LambdaQueryWrapper<DeclarePlan> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        if(declarePlan.getPlanId() != null){
            queryWrapper.eq(DeclarePlan::getPlanId, declarePlan.getPlanId());
        }
        if(declarePlan.getDeclareId() != null){
            queryWrapper.eq(DeclarePlan::getDeclareId, declarePlan.getDeclareId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDeclarePlan(DeclarePlan declarePlan) {
        this.save(declarePlan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeclarePlan(DeclarePlan declarePlan) {
        this.saveOrUpdate(declarePlan);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDeclarePlans(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

     @Override
     public List<EvaluationResultVO> findEvaluationResultByPlanId(Long planId){
         return baseMapper.findEvaluationResultByPlanId(planId);
    }

    @Override
    public DeclarePlan findByDeclareTopicIdAndPlanId(Long declareTopicId, Long planId) {
        return baseMapper.findByDeclareTopicIdAndPlanId(declareTopicId, planId);
    }

    /**
     * 根据申请课题id查询所有的计划
     * @param declareTopicId
     * @return
     */
    @Override
    public List<DeclarePlan> getAllPlanIdByDeclareTopicId(Long declareTopicId) {
        return baseMapper.findByDeclareTopicIdOrderByApplyCount(declareTopicId);
    }

}
